﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using RuoVea.ExConfig;
using RuoVea.OmiApi.Upload.Service;
using RuoVea.OmiApi.Upload.Service.Dto;

namespace RuoVea.OmiApi.Upload;

/// <summary>
/// 注入扩展
/// </summary>
public static class ServiceCollectionExtensions
{
    /// <summary>
    /// 文件上传管理
    /// </summary>
    /// <param name="services"></param>
    /// <param name="serviceLifetime">Scoped</param>
    /// <returns></returns>
    /// <exception cref="ArgumentNullException"></exception>
    public static IServiceCollection AddOmiUploadSetup(this IServiceCollection services, ServiceLifetime serviceLifetime = ServiceLifetime.Scoped)
    {
        if (services == null) throw new ArgumentNullException(nameof(services));
        services.Configure<UploadConfig>(AppSettings.GetSection("UploadConfig"));
        return DI(services, null, serviceLifetime);
    }

    /// <summary>
    /// 文件上传管理
    /// </summary>
    /// <param name="services"></param>
    /// <param name="config"></param>
    /// <param name="serviceLifetime">Scoped</param>
    /// <returns></returns>
    /// <exception cref="ArgumentNullException"></exception>
    public static IServiceCollection AddOmiUploadSetup(this IServiceCollection services, IConfiguration config, ServiceLifetime serviceLifetime = ServiceLifetime.Scoped)
    {
        if (services == null) throw new ArgumentNullException(nameof(services));
        if (config == null) throw new ArgumentNullException(nameof(config));
        services.Configure<UploadConfig>(config);
        UploadConfig dbInitConfig = services.BuildServiceProvider().GetRequiredService<IOptions<UploadConfig>>()?.Value;
        return DI(services, dbInitConfig, serviceLifetime);
    }

    /// <summary>
    /// 文件上传管理
    /// </summary>
    /// <param name="services"></param>
    /// <param name="config"></param>
    /// <param name="serviceLifetime">Scoped</param>
    /// <returns></returns>
    /// <exception cref="ArgumentNullException"></exception>
    public static IServiceCollection AddOmiUploadSetup(this IServiceCollection services, Action<UploadConfig> config, ServiceLifetime serviceLifetime = ServiceLifetime.Scoped)
    {
        if (services == null) throw new ArgumentNullException(nameof(services));
        if (config == null) throw new ArgumentNullException(nameof(config));
        services.Configure(config);
        UploadConfig dbInitConfig = services.BuildServiceProvider().GetRequiredService<IOptions<UploadConfig>>()?.Value;
        return DI(services, dbInitConfig, serviceLifetime);
    }

    #region DI
    private static IServiceCollection DI(IServiceCollection services, UploadConfig dbInitConfig, ServiceLifetime serviceLifetime)
    {
        services.AddHttpContextAccessor();

        switch (serviceLifetime)
        {
            case ServiceLifetime.Scoped:
                services.AddScoped<FileUploadService>();
                break;
            case ServiceLifetime.Singleton:
               
                services.AddSingleton<FileUploadService>();
                break;
            case ServiceLifetime.Transient:
           
                services.AddTransient<FileUploadService>();
                break;
        }
        return services;
    }
    #endregion
}
